package liuwanghui.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import liuwanghui.dto.request.*;
import liuwanghui.pojo.Result;
import liuwanghui.service.OrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/orders")
@RequiredArgsConstructor
@Tag(name = "订单管理", description = "订单相关接口")
public class OrderController {

    private final OrderService orderService;

    @GetMapping
    @Operation(summary = "获取订单列表")
    public Result getOrders(@ModelAttribute OrderQueryDTO params) {
        return orderService.getOrders(params);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取订单详情")
    public Result getOrderDetail(
            @PathVariable Long id,
            @RequestParam(required = false) Boolean includeHistory) {
        return orderService.getOrderDetail(id, includeHistory);
    }

    @PutMapping("/{id}/status")
    @Operation(summary = "更新订单状态")
    public Result updateOrderStatus(
            @PathVariable Long id,
            @RequestBody @Valid OrderStatusDTO dto) {
        orderService.updateOrderStatus(id, dto);
        return Result.success();
    }

    @PostMapping("/{id}/cancel")
    @Operation(summary = "取消订单")
    public Result cancelOrder(
            @PathVariable Long id,
            @RequestBody @Valid OrderCancelDTO dto) {
        orderService.cancelOrder(id, dto);
        return Result.success();
    }

    @PostMapping("/{id}/ship")
    @Operation(summary = "确认发货")
    public Result shipOrder(
            @PathVariable Long id,
            @RequestBody @Valid OrderShipDTO dto) {
        orderService.shipOrder(id, dto);
        return Result.success();
    }

    @GetMapping("/recent")
    @Operation(summary = "获取最近订单")
    public Result getRecentOrders(
            @RequestParam String role,
            @RequestParam(required = false, defaultValue = "7") Integer days,
            @RequestParam(required = false, defaultValue = "10") Integer limit) {
        return orderService.getRecentOrders(role, days, limit);
    }
} 